package Aptech.DAL;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import Aptech.DALInterface.OderInterface;
import HibernateMappingPoJo.Builts;
import HibernateMappingPoJo.Orders;

public class MgOrder implements OderInterface {

	private SessionFactory sessFact;
    private Session sess;
    private Transaction tr;
	
	@Override
	public List getAllOder() {
		List list = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            list = sess.createCriteria(Orders.class).list();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }

        return list;
	}

	@Override
	public void addOrder(Orders order) {
		 try {
	            sessFact = Hibernate.HibernateUtil.getSessionFactory();
	            sess = sessFact.openSession();
	            tr = sess.beginTransaction();
	            sess.save(order);
	            tr.commit();
	            sess.flush();
	        } catch (HibernateException he) {

	            System.out.println(he.getMessage());

	        } finally {
	            sess.close();

	        }
		
	}

	@Override
	public void deleteOrder(String id) {
		 try {
	            sessFact = Hibernate.HibernateUtil.getSessionFactory();
	            sess = sessFact.openSession();
	            String sql = "delete Orders where ID = '" + id + "'";
	            sess.createSQLQuery(sql).executeUpdate();
	            sess.flush();
	        } catch (HibernateException he) {
	            System.out.println(he.getMessage());
	        } finally {
	            sess.close();
	        }
		
	}

	@Override
	public void editOrder(Orders order) {
		try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            tr = sess.beginTransaction();

            sess.update(order);
            tr.commit();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();

        }
		
	}

	@Override
	public Orders getOrderByID(String id) {
		Orders order = null;
        try {
            sessFact = Hibernate.HibernateUtil.getSessionFactory();
            sess = sessFact.openSession();
            Criteria criteria = sess.createCriteria(Orders.class).add(Restrictions.eq("id", id));
            order = (Orders) criteria.uniqueResult();
            sess.flush();
        } catch (HibernateException he) {
            System.out.println(he.getMessage());
        } finally {
            sess.close();
        }
        return order;
    }
	

}
